package service

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"push/config/table"
	"push/pool"

	//"push/config/table"
	//"push/pool"
	"sync"
	"time"
)

var chMaxGo chan int
var oSynWait sync.WaitGroup

func Produce(){
	chMaxGo = make(chan int, 500)
	fmt.Println("running...")
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
	c := pool.InitRedisPool()
	for i := 0; i <= 10000; i++ {
		oSynWait.Add(1)
		chMaxGo <- 1
		go logic(i,c)
	}
	oSynWait.Wait()
	close(chMaxGo)
	fmt.Println("end...")
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
}

func logic(i int,RedisPoolConn *redis.Pool){
	defer func() {
		oSynWait.Done()
		time.Sleep(time.Millisecond * 500)
		<-chMaxGo
	}()
	redisgo(i,RedisPoolConn)
	return
}

func redisgo(i int,RedisPoolConn *redis.Pool){
	//redis_conn := pool.RedisConn()
	//pool.InitRedisPool()
	//redis_conn := pool.GetRedisPoolConn()
	//pool.RedisLpush(redis_conn, table.Redis_table_hongbao_list,i)
	//pool.RedisConnClose(redis_conn)
	//redis_conn := pool.RedisConn()
	////pool.InitRedisPool()
	////redis_conn := pool.GetRedisPoolConn()
	//pool.RedisLpush(redis_conn, table.Redis_table_hongbao_list,i)
	//pool.RedisConnClose(redis_conn)

	c := pool.GetRedisPoolConn(RedisPoolConn)
	pool.RedisLpush(c, table.Redis_table_hongbao_list,i)
	c.Close()
	//pool.RedisConnClose(c)
	fmt.Println(RedisPoolConn.ActiveCount(),RedisPoolConn.Stats(),RedisPoolConn.IdleCount())
}

